package com.lagou.controller;

import com.lagou.domain.*;
import com.lagou.service.MenuService;
import com.lagou.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RolesController {

    @Autowired
    private RolesService rolesService;

    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role) {

        List<Role> roles = rolesService.findAllRole(role);

        ResponseResult result = new ResponseResult(true, 200, "查询角色列表成功", roles);

        return result;
    }

    @Autowired
    private MenuService menuService;

    @RequestMapping("/findAllMenu")
    public ResponseResult findSubMenuListByPid() {

        List<Menu> menus = menuService.findSubMenuListByPid(-1);

        HashMap<String, Object> map = new HashMap<>();
        map.put("parentMenuList", menus);

        ResponseResult result = new ResponseResult(true, 200, "获取菜单成功", map);

        return result;
    }

    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId) {

        List<Integer> menuByRoleId = rolesService.findMenuByRoleId(roleId);

        ResponseResult result = new ResponseResult(true, 200, "查询menu id成功", menuByRoleId);

        return result;
    }


    @RequestMapping("/roleContextMenu")
    public ResponseResult roleContextMenu(@RequestBody RoleMenuVo roleMenuVo) {

        rolesService.roleContextMenu(roleMenuVo);

        ResponseResult result = new ResponseResult(true, 200, "响应成功", null);

        return result;
    }

    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id) {

        rolesService.deleteRole(id);

        ResponseResult result = new ResponseResult(true, 200, "删除角色成功", null);

        return result;
    }

    /**
     * 给角色分配资源
     * @param roleResourceVo
     * @return
     */
    @RequestMapping("/roleContextResource")
    public ResponseResult roleContextResource(@RequestBody RoleResourceVo roleResourceVo) {

        rolesService.roleContextResource(roleResourceVo);

        return new ResponseResult(true, 200, "给角色分配资源成功", null);
    }

    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(Integer roleId) {

        List<ResourceCategory> resourceList = rolesService.findResourceListByRoleId(roleId);

        return new ResponseResult(true, 200, "响应成功", resourceList);
    }
}
